<?php

namespace App\Http\Controllers;

use App\Common\Constant\Params;
use App\Common\Constant\Status;
use App\Services\IFinanceService;
use App\Services\ISessionService;
use App\Services\IStatisticService;
use App\Services\ISystemService;
use App\Services\IWalletService;
use App\Services\IWeixinService;
use Carbon\Carbon;
use Illuminate\Http\Request;

/**
 * 师傅钱包
 * Class WalletController
 * @package App\Http\Controllers
 */
class WalletController extends BaseController
{

    /**
     * 首页
     */
    public function index(Request $request, IWalletService $walletService)
    {
        $wallet = $walletService->getWalletByUserid(getCurrentUserid());
        return view('wallet/index',['wallet'=>$wallet]);
    }

    /**
     * 钱包密码
     */
    public function password(Request $request, ISessionService $sessionService, IWalletService $walletService)
    {
        if ($request->isMethod('post')){
            $ret = $walletService->changePassword(getCurrentUserid(),$request->input('wallet_password'));
            if (!$ret){
                return $this->renderFail(Status::MASTER_WALLET_PASSWORD_CHANGE);
            }
            return redirect($sessionService->getRedirectUrl('wallet'));
        }

        $user = getCurrentUser();
        $wallet = $walletService->getWalletByUserid(getCurrentUserid());
        return view('wallet/password',['wallet'=>$wallet,'phone'=>$user->phone]);
    }

    /**
     * 账单列表
     */
    public function bill(Request $request, IStatisticService $statisticService)
    {

        $stat = getCurrentUser()->stat;
        $bill_list = $statisticService->lastThreeMonthBillList(getCurrentUserid());

        $masterStat = [
            'order_count'=>$stat->order_count,
            'income_sum'=>$stat->income_sum,
            'three_month_order_count'=>$bill_list->sum('order_count'),
            'three_month_income_sum'=>$bill_list->sum('income_sum'),
        ];

        return view('wallet/bill',['masterStat'=>$masterStat,'list'=>$bill_list]);
    }

    /**
     * 周账单明细
     */
    public function billDetail($start_time,Request $request, IStatisticService $statisticService)
    {
        $billdetail = $statisticService->weekBill(getCurrentUserid(),$start_time);
        $billdetailList = $statisticService->billDetailList(getCurrentUserid(),$start_time);
        return view('wallet/billDetail',['detail'=>$billdetail,'list'=>$billdetailList]);
    }

    /**
     * 提现
     */
    public function cash(Request $request, ISessionService $sessionService, IWalletService $walletService)
    {
        $wallet = $walletService->getWalletByUserid(getCurrentUserid());

        return view('wallet/cash',['alipay_name'=>$wallet->alipay_name ? : $wallet->alipay_account]);
    }

    /**
     * 提现账号绑定
     */
    public function accountBind(Request $request, ISessionService $sessionService, IWalletService $walletService)
    {
        if ($request->isMethod('post')){
            $ret = $walletService->alipayAccountBind(getCurrentUserid(),$request->input('alipay_name'),$request->input('alipay_account'));
            if (!$ret){
                return $this->renderFail(Status::MASTER_WALLET_ACCOUNT_BIND);
            }
            return redirect($sessionService->getRedirectUrl('wallet/cash'));
        }

        return view('wallet/accountBind',['master'=>getCurrentUser()]);
    }

    /**
     * 提现申请发起
     */
    public function cashStart(Request $request, ISessionService $sessionService, IWalletService $walletService,ISystemService $systemService)
    {
        $master_id = getCurrentUserid();
        if (!$walletService->issetPassword(getCurrentUserid())){
            $systemService->alert('未设置钱包密码，请先设置钱包密码');
            return redirect('wallet/password');
        }
        $wallet = $walletService->getWalletByUserid($master_id);
        return view('wallet/cashStart',['master'=>getCurrentUser(),
            'wallet'=>$wallet,
            'cashingAmount'=>$walletService->getCashingAmount($master_id),
            'cash_minimum'=>config('common.cash_minimum',1),
        ]);
    }

    /**
     * 诚信保证金
     */
    public function assureFund(Request $request, IWalletService $walletService)
    {
        $wallet = $walletService->getWalletByUserid(getCurrentUserid());
        return view('wallet/assureFund',['wallet'=>$wallet,
            'trade_type'=>Params::TRADE_TYPE_ASSURE,
            'assure_fund_minimum'=>config('common.assure_fund_minimum',200),
            'default_pay_method'=>config('common.default_pay_method',Params::PAY_METHOD_WEIXIN)
        ]);
    }

    /**
     * 诚信保证金记录
     */
    public function assureFundHistory(Request $request, IWalletService $walletService,IFinanceService $financeService)
    {
        $list = $financeService->masterFundHistory(getCurrentUserid());
        return view('wallet/assureFundHistory',['list'=>$list,
            'master_fund_waiting'=>Params::MASTER_FUND_WAITING
        ]);
    }

    /**
     * 申请退还诚信保证金
     */
    public function returnAssureFund(Request $request, IWalletService $walletService,ISystemService $systemService)
    {
        if ($request->isMethod('post')){
            $master_id = getCurrentUserid();
//            if (!$walletService->issetAlipayAccount($master_id)){
//                $systemService->alert('未设置提现账户，请先设置提现账户');
//                return redirect(url('wallet/accountbind').'?redirectUrl='.url('wallet/assurefund'));
//            }
            $walletService->returnAssureFund($master_id);
        }

        return back();
    }

    /**
     * 钱包明细记录
     */
    public function history(Request $request, IWalletService $walletService,IFinanceService $financeService)
    {
        $list = $financeService->masterWalletHistory(getCurrentUserid());
        return view('wallet/history',['list'=>$list,
            'trade_type_cash'=>Params::TRADE_TYPE_CASH,
            'master_cash_review'=>Params::MASTER_CASH_REVIEW
        ]);
    }

}
